﻿using Exam.infranstuctrue.Dto.Request.Stage;
using Exam.infranstuctrue.Dto.Request.StageQuestion;
using Exam.infranstuctrue.Tools;
using Exam.Service.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO;

namespace Exam.Controllers
{
    [ApiController]
    public class StageController : BaseController
    {
        private readonly IStageServer _stageServer;
        private readonly Session _session;

        public StageController(IStageServer stageServer, Session session)
        {
            _stageServer = stageServer;
            _session = session;
        }
        /// <summary>
        /// 获取所有关卡
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <param name="IsBan"></param>
        /// <param name="IsPublic"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetStage(int page = 1, int limit = 10, string? key = "", int IsBan = -1, int IsPublic = -1)
        {
            key = key ?? "";
            var token = _session.GetToken();
            var result = await _stageServer.GetStage(token, page, limit, key, IsBan, IsPublic);
            return Ok(result);
        }
        /// <summary>
        /// 我的图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult MyFile()
        {
            try
            {
                string name = Request.Form["file"];//上传类型

                var formFile = Request.Form.Files[0];//获取请求发送过来的文件
                                                     // var webRootPath = @"/www/Files";
                var webRootPath = @"E:\S4\KoalaStudy\koalastady\KoalaStudy.Api\KoalaStudy.Exam\Exam\Exam\Img";
                var filePath = $"/{DateTime.Now:yyyyMMdd}/";
                //创建每日存储文件夹
                if (!Directory.Exists(webRootPath + filePath))
                {
                    Directory.CreateDirectory(webRootPath + filePath);
                }

                if (formFile != null)
                {
                    //文件后缀
                    var fileExtension = Path.GetExtension(formFile.FileName);//获取文件格式，拓展名

                    //判断文件大小
                    var fileSize = formFile.Length;
                    int maxSize = 1024 * 1024 * 1024;//500MB

                    if (fileSize <= 0)
                    {
                        return new JsonResult(new { code = -1, msg = $"你选择的是空文件" });
                    }

                    if (fileSize > maxSize) //10M TODO:(1mb=1024X1024b)
                    {
                        return new JsonResult(new { code = -1, msg = $"上传的文件不能大于【{maxSize / 1024 / 1024}MB】" });
                    }
                    //保存的文件名称(以名称和保存时间命名)
                    var saveName = $"{Config.GUID()}" + fileExtension;

                    //文件保存
                    using (var fs = System.IO.File.Create(webRootPath + filePath + saveName))
                    {
                        formFile.CopyTo(fs);
                        fs.Flush();
                    }

                    //完整的文件路径
                    var completeFilePath = Path.Combine(filePath + saveName);

                    var website = "http://wjgapi.shinyeeun.site/file/";
                    return Ok(website + completeFilePath);
                }
                return NotFound("未知错误");
            }
            catch (Exception e)
            {
                return Ok(e.Message);
            }

        }
        /// <summary>
        /// 添加关卡
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddStage(StageDto dto)
        {
            dto.Sphoto = MyFile().ToString();
            var result = await _stageServer.AddStage(dto);
            return Ok(result);
        }
        /// <summary>
        /// 删除关卡
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> DeleteStage(string sid)
        {
            var result = await _stageServer.DeleteStage(sid);
            return Ok(result);
        }
        /// <summary>
        /// 获取单个关卡 
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetEchoStage(string? sid)
        {
            var result = await _stageServer.GetEchoStage(sid);
            return Ok(result);
        }
        /// <summary>
        /// 修改关卡
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateStage(StageDto dto)
        {
            dto.Sphoto = MyFile().ToString();
            var result = await _stageServer.UpdateStage(dto);
            return Ok(result);
        }
        /// <summary>
        /// 是否禁用
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> StageIsBan(List<StageDto> dto)
        {
            var result = await _stageServer.StageIsBan(dto);
            return Ok(result);
        }
        /// <summary>
        /// 是否发布
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> StageIsPublish(List<StageDto> dto)
        {
            var result = await _stageServer.StageIsPublish(dto);
            return Ok(result);
        }
        /// <summary>
        /// 添加子集关卡
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddChilrenStage(StageDetaliDto dto)
        {
            dto.Sdphoto = MyFile().ToString();
            var result = await _stageServer.AddChilrenStage(dto);
            return Ok(result);
        }
        /// <summary>
        /// 删除子集关卡
        /// </summary>
        /// <param name="sdid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> DeleteStageChilren(string? sdid)
        {
            var result = await _stageServer.DeleteStageChilren(sdid);
            return Ok(result);
        }
        /// <summary>
        /// 获取单个子集关卡
        /// </summary>
        /// <param name="sdid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetEchoStageChilren(string? sdid)
        {
            var result = await _stageServer.GetEchoStageChilren(sdid);
            return Ok(result);
        }
        /// <summary>
        /// 修改关卡子集
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateStageChilren(StageDetaliDto dto)
        {
            dto.Sdphoto = MyFile().ToString();
            var result = await _stageServer.UpdateStageChilren(dto);
            return Ok(result);
        }
        /// <summary>
        /// 子集是否禁用
        /// </summary>
        /// <param name="sdid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ChilrenIsBan(string? sdid)
        {
            var result = await _stageServer.ChilrenIsBan(sdid);
            return Ok(result);
        }
        /// <summary>
        /// 获取指定试题
        /// </summary>
        /// <param name="lable"></param>
        /// <param name="lv"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetQuestion(string lable, int lv, int page, int limit, string? key = "")
        {
            key = key ?? "";
            var result = await _stageServer.GetQuestion(lable, lv, page, limit, key);
            return Ok(result);
        }
        /// <summary>
        /// 获取关卡内试题
        /// </summary>
        /// <param name="sdid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> StageGetQuestion(string? sdid)
        {
            var result = await _stageServer.StageGetQuestion(sdid);
            return Ok(result);
        }
        /// <summary>
        /// 添加关卡试题
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> StageAddQuestion(StageQuestionDto dto)
        {
            var result = await _stageServer.StageAddQuestion(dto);
            return Ok(result);
        }
        /// <summary>
        /// 删除关卡试题
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> StageDeteleQuestion(StageQuestionDto dto)
        {
            var result = await _stageServer.StageDeteleQuestion(dto);
            return Ok(result);
        }

    }
}
